﻿Imports System.Reflection

Public Class CDB
    Shared cn As New ADODB.Connection

    Public Sub New()
        MyBase.New()
        Dim strType As String
        strType = ""
        strType = System.Configuration.ConfigurationManager.AppSettings("CONS")
        If strType <> "" Then
            strConn = System.Configuration.ConfigurationManager.AppSettings(strType)
            '2009-07-19
            If (strType = "SQLITEDBTYPE") Then
                Dim strPath As String
                'D-2009-09-10
                'strPath = Environment.CurrentDirectory + "\SQLITEDATA\XSC.db"
                'A-2009-09-10
                strPath = System.IO.Path.Combine(GetCurrentDirectory, "SQLITEDATA\XSC.db")
                If (strConn.Contains("$SQLITEDATA")) Then
                    strConn = strConn.Replace("$SQLITEDATA", strPath)
                End If
            End If
            '2009-07-19
        End If
        'cException = GetException() 'D-2009-10-11
    End Sub

#Region "该类为单例模式 A-2009-10-11"
    Private Shared _instance As CDB = Nothing
    Private Shared ReadOnly padlock As New Object
    Public Shared ReadOnly Property Instance() As CDB
        Get
            'Lock(padlock)
            If _instance Is Nothing Then
                _instance = New CDB
            End If
            'Unlock(padlock)
            Return _instance
        End Get
    End Property
#End Region

    Public strConn As String
    'Private cException As Object   '保存异常类 'D-2009-10-11

    '#Region "保存异常"
    '    Public Sub SaveException(ByVal e As Exception)
    '        'Dim strPath As String
    '        'strPath = System.Configuration.ConfigurationManager.AppSettings("ExceptionFile")
    '        'Dim sw As System.IO.StreamWriter
    '        'If (Not (System.IO.File.Exists(strPath))) Then
    '        '    sw = System.IO.File.CreateText(strPath)
    '        'End If

    '        'sw = New System.IO.StreamWriter(strPath, True)
    '        'If sw IsNot Nothing Then
    '        '    sw.WriteLine(e.ToString())
    '        '    sw.Close()
    '        'End If
    '        'cException.SaveExcetpion(e) 'D-2009-10-11
    '    End Sub

    '#End Region

    Public Function GetCn() As ADODB.Connection

        GetCn = Nothing
        Try

            If cn Is Nothing Then
                cn = New ADODB.Connection
                cn.ConnectionString = strConn
            End If

            If cn.State = ADODB.ObjectStateEnum.adStateOpen Then
                Return cn
            End If

            If cn.State = ADODB.ObjectStateEnum.adStateClosed Then
                cn.ConnectionString = strConn
                cn.Open()
            End If
            GetCn = cn
        Catch ex As Exception
            'SaveException(ex) 'D-2009-10-11
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
        End Try

    End Function

    Public Function CloseCn(ByVal cn As ADODB.Connection) As Boolean
        CloseCn = False
        Try
            If cn Is Nothing Then Exit Function
            If cn.State = ADODB.ObjectStateEnum.adStateOpen Then
                cn.Close()
            End If
            CloseCn = True
        Catch ex As Exception
            'SaveException(ex)'D-2009-10-11
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
        End Try
    End Function

    'A-2009-09-13
    Public Function CloseCn() As Boolean
        CloseCn = False
        Try
            If cn Is Nothing Then Exit Function
            If cn.State = ADODB.ObjectStateEnum.adStateOpen Then
                cn.Close()
            End If
            cn = Nothing
            CloseCn = True
            GC.Collect()
        Catch ex As Exception
            'SaveException(ex) 'D-2009-10-11
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
        End Try
    End Function
    ''A-2009-09-13
    '''' <summary>
    '''' 得到异常处理类
    '''' </summary>
    '''' <returns></returns>
    '''' <remarks></remarks>
    'Private Function GetException() As Object
    '    GetException = Nothing
    '    Dim strd1 As String
    '    Dim strName As String
    '    strd1 = System.Configuration.ConfigurationManager.AppSettings("DLLUTILDIRECTORY")
    '    '表示当前程序目录 2009-09-05
    '    If strd1 = "\" Then
    '        strd1 = System.IO.Directory.GetCurrentDirectory()
    '    End If
    '    '表示当前程序目录 2009-09-05
    '    strName = System.Configuration.ConfigurationManager.AppSettings("DLLUTILNAME")
    '    Dim strPath As String
    '    strPath = strd1 + "\" + strName
    '    If (System.IO.File.Exists(strPath)) Then
    '        Dim tempas As System.Reflection.Assembly
    '        tempas = System.Reflection.Assembly.LoadFile(strPath)

    '        Dim ty As System.Type = tempas.GetType("UTIL.CException")
    '        '创建实例
    '        GetException = System.Activator.CreateInstance(ty)
    '    End If
    'End Function

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub


#Region "得到当前的目录 2009-09-10"
    Public Function GetCurrentDirectory() As String
        GetCurrentDirectory = ""
        GetCurrentDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase
        If GetCurrentDirectory.EndsWith("\\") Or GetCurrentDirectory.EndsWith("/") Then
            GetCurrentDirectory = GetCurrentDirectory.Substring(0, GetCurrentDirectory.Length - 1)
        End If
    End Function
#End Region

#Region "得到某一个数据库的连接 A-2009-12-23"
    Public Shared Function GetCn(ByVal strDbPath As String) As ADODB.Connection
        Dim strType As String
        Dim strConn1 As String = ""
        strType = ""
        strType = System.Configuration.ConfigurationManager.AppSettings("CONS")
        strConn1 = System.Configuration.ConfigurationManager.AppSettings(strType)
        strConn1 = strConn1.Replace("$SQLITEDATA", strDbPath)
        GetCn = Nothing
        Try

            If GetCn Is Nothing Then
                GetCn = New ADODB.Connection
                GetCn.ConnectionString = strConn1
            End If

            If GetCn.State = ADODB.ObjectStateEnum.adStateOpen Then
                Return cn
            End If

            If GetCn.State = ADODB.ObjectStateEnum.adStateClosed Then
                GetCn.ConnectionString = strConn1
                GetCn.Open()
            End If
        Catch ex As Exception
            'SaveException(ex) 'D-2009-10-11
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
        End Try

    End Function
#End Region

    Public Sub tEST()
      
    End Sub
End Class


